import utils from "./utils";
import config from "@/common/config";

const uniApi = {
	/**
	 * @param {Object} url 需要跳转的应用内非 tabBar 的页面的路径
	 * @param {Object} animationType 窗口显示的动画效果
	 * @param {Object} animationDuration 窗口动画持续时间，单位为 ms
	 */

	to(url) {
		uni.navigateTo({
			url,
		});
	},

	// 关闭当前页面，跳转到应用内的某个页面。
	rt(url) {
		uni.redirectTo({
			url,
		});
	},

	// 关闭所有页面，打开到应用内的某个页面。
	rl(url) {
		uni.reLaunch({
			url,
		});
	},

	/**
	 * 打开任意链接
	 * @param {Object} url 页面地址
	 */
	on(url) {
		// url = decodeURIComponent(url);
		// 关闭所有页面，跳转链接
		// if (url == '/pages/tabBar/index' || url == '/pages/tabBar/my' || url == '/pages/tabBar/order') {
		uni.switchTab({
			url: url,
		});
		// } else {
		// 	this.to(url);
		// }
	},

	/**
	 * 页面返回
	 * @param {Object} num 返回页面数量
	 */
	cback(num) {
		uni.navigateBack({
			delta: num ? num : 1,
		});
	},

	/**
	 * 页面认证
	 * @param {Object} url 地址
	 */
	auth(url) {
		this.to(store.state.user.isLogin ? url : "/pages/auth/login");
	},

	/**
	 * 预览图片
	 * @param { Array | String }	urls	图片路径数组|单张图片路径
	 * @param { Number | String }	current	  预览下标
	 * @param { String }	variable   预览变量名
	 * @param { Object }	other	其他配置
	 * @return { uniPromis }
	 */
	previewImage(urls = [], current = 0, variable = "url", other = {}) {
		console.log("urls", urls);
		if (utils.isString(urls)) {
			urls = [urls];
		}
		if (utils.isObject(urls[0])) {
			urls = urls.map((item) => {
				return item[variable];
			});
		}
		console.log("urls", urls);
		return uni.previewImage({
			urls,
			current,
			...other,
		});
	},

	// 图片上传
	uploadImage(chooseConfig = {}, uploadConfig = {}) {
		const {
			other = {}, name
		} = uploadConfig;
		return this.chooseImage(chooseConfig).then((res) => {
			let fileArr = [];
			res.forEach((item, index) => {
				fileArr.push(this.uploadFile(item, other, name));
			});
			console.log("fileArr", fileArr);
			return Promise.all(fileArr);
		});
	},

	// 选择上传图片事件
	chooseImage(other = {}) {
		return new Promise((resolve, reject) => {
			uni.chooseImage({
				...other,
				success(res) {
					resolve(res.tempFilePaths);
				},
			});
		});
	},

	// 上传文件
	uploadFile(filePath = "", other = {}, name = "file") {
		uni.showLoading({
			title: "上传中",
		});
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: `${config.appurl}/ebapi/public_api/upload`,
				filePath,
				name,
				// header: {
				// 	token: uni.getStorageSync("userInfo")?.token ||
				// 		"ee66d660-0776-4dcc-bb2f-3bd3096349a8",
					
				// },
				...other,
				success: (res) => {
					if (utils.isString(res.data)) {
						res.data = utils.json(res.data);
					}
					uni.hideLoading();
					if (res.data.code == 200) {
						resolve(res.data.data.url);
					} else {
						this.msg("上传失败");
						resolve(filePath);
					}
				},
			});
		});
	},

	//统一提示方便全局修改
	msg(title, duration = 1500, mask = false, icon = "none") {
		if (Boolean(title) === false) {
			return;
		}
		uni.showToast({
			title,
			duration,
			mask,
			icon,
		});
	},
};
export default uniApi;